package com.school.service.impl;

import com.school.entity.QueryRecord;
import com.school.repository.QueryRecordRepository;
import com.school.service.QueryRecordService;
import com.school.service.RealisticRecordService;
import com.school.untils.AuthMDA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class QueryRecordServiceImp implements QueryRecordService {
    @Autowired
    QueryRecordRepository queryRecordRepository;
    @Autowired
    RealisticRecordService realisticRecordService;
    @Value("${web.upload.path}")
    String basePath;

    /**
     * 增加
     * @param queryRecord
     * @return
     */
    @Override
    public QueryRecord addQueryRecord(List<MultipartFile> fileList, QueryRecord queryRecord) throws IOException {
        queryRecord.setFiles(AuthMDA.removeBrackets(AuthMDA.setFiles(fileList,basePath).toString()));
        queryRecord.setComplainantsTime(new Date());
        realisticRecordService.updateAudit(queryRecord.getRecordId(),3);
        return queryRecordRepository.save(queryRecord);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public List<QueryRecord> getDetailsByRecordId(String id) {
        return queryRecordRepository.findQueryRecordsByRecordId(id);
    }

    /**
     * 更新
     * @param queryRecord
     */
    @Override
    public QueryRecord updateQueryRecord(QueryRecord queryRecord,Integer audit) {
        QueryRecord queryRecords = queryRecordRepository.findById(queryRecord.getId()).get();
        if(!StringUtils.isEmpty(queryRecord.getPrecessStatusSt())){
            queryRecords.setPrecessStatusSt(queryRecord.getPrecessStatusSt());
        }
        if(!StringUtils.isEmpty(queryRecord.getPrecessStatusTh())){
            queryRecords.setPrecessStatusTh(queryRecord.getPrecessStatusTh());
        }
        if(!StringUtils.isEmpty(queryRecord.getPrecessStatusSc())){
            queryRecords.setPrecessStatusSc(queryRecord.getPrecessStatusSc());
        }
        if(!StringUtils.isEmpty(queryRecord.getProcessRate())){
            queryRecords.setProcessRate(queryRecord.getProcessRate());
        }
        if(!StringUtils.isEmpty(queryRecord.getProcessMode())){
            queryRecords.setProcessMode(queryRecord.getProcessMode());
        }
        if(!StringUtils.isEmpty(queryRecord.getInstruction())){
            queryRecords.setInstruction(queryRecord.getInstruction());
        }
        if(!StringUtils.isEmpty(queryRecord.getProofFiles())){
            queryRecords.setProofFiles(queryRecord.getProofFiles());
        }
        queryRecords.setPrecessTime(new Date());
        realisticRecordService.updateAudit(queryRecord.getRecordId(),audit);
        return queryRecordRepository.saveAndFlush(queryRecords);
    }

    /**
     * 处理状态变更
     * @param queryRecord
     */
    @Override
    public void updateAudit(QueryRecord queryRecord) {
        QueryRecord queryRecords = queryRecordRepository.findById(queryRecord.getId()).get();
        if(!StringUtils.isEmpty(queryRecord.getPrecessStatusSt())){
            queryRecords.setPrecessStatusSt(queryRecord.getPrecessStatusSt());
        }
        if(!StringUtils.isEmpty(queryRecord.getPrecessStatusTh())){
            queryRecords.setPrecessStatusTh(queryRecord.getPrecessStatusTh());
        }
        if(!StringUtils.isEmpty(queryRecord.getPrecessStatusSc())){
            queryRecords.setPrecessStatusSc(queryRecord.getPrecessStatusSc());
        }
        if(!StringUtils.isEmpty(queryRecord.getProcessRate())){
            queryRecords.setProcessRate(queryRecord.getProcessRate());
        }
        queryRecordRepository.saveAndFlush(queryRecords);
    }
}
